﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class LuaScripts
{
    public string rootPath = "";
    /// <summary>
    /// 包名 对应 lua脚本和脚本内容
    /// </summary>
    Dictionary<string, Dictionary<string, string>> luaTable = null;
    private string packageName;
    public LuaScripts()
    {
        luaTable = new Dictionary<string, Dictionary<string, string>>();
    }
    #region 加载lua脚本
    public void Load(string local)
    {
        packageName = "LocalPackage";
        string[] scriptPaths= FileUnit.GetDirFiles(local);
        foreach (string scriptPath in scriptPaths)
        {
            if (scriptPath.LastIndexOf(".lua") < 0) continue;
            string fn = scriptPath.Substring(scriptPath.LastIndexOf(rootPath) + rootPath.Length + 1);
            fn = fn.Substring(0, fn.LastIndexOf("."));
            fn = fn.ToLower();
            StreamReader streamReader = new StreamReader(scriptPath, Encoding.UTF8);
            string countent = FilterNotes(streamReader.ReadToEnd());
            Dictionary<string, string> luaScripts = null;
            if (!luaTable.ContainsKey(packageName))
            {
                luaScripts = new Dictionary<string, string>();
                luaTable.Add(packageName, luaScripts);
            }
            else
                luaScripts = luaTable[packageName]; 

            if (!luaScripts.ContainsKey(fn))
            {
                luaScripts.Add(fn, countent);
            }
            streamReader.Close();
            streamReader.Dispose();
        }
    }
    public void LoadAssetBundle(AssetBundle asset)
    {
        if (asset == null) return;
        packageName = PathUnit.GetPlatformName();
        string[] allNames = asset.GetAllAssetNames();
        if(allNames!=null && allNames.Length > 0)
        {
            Dictionary<string, string> luaScripts = null;
            if (!luaTable.ContainsKey(packageName))
            {
                luaScripts = new Dictionary<string, string>();
                luaTable.Add(packageName, luaScripts);
            }
            else
                luaScripts = luaTable[packageName];
            //获取全部脚本
            foreach (string name in allNames)
            {
                TextAsset content = asset.LoadAsset<TextAsset>(name);
                if(content != null)
                {
                    string fn = name.Substring(name.LastIndexOf(rootPath) + rootPath.Length + 1);
                    fn = fn.Substring(0, fn.LastIndexOf("."));
                    fn = fn.ToLower();
                    string countent = FilterNotes(content.text)?.Trim();
                    if (!luaScripts.ContainsKey(fn))
                    {
                        luaScripts.Add(fn, countent);
                    }

                }
            }
        }
    }
    string FilterNotes(string content)
    {
        content = Tools.MidStrEx(content, "--[[", "]]");
        //content = Tools.MidStrEx(content, "--", "\r\n");
        return content.Trim();
    }
    #endregion

    #region 获取Lua脚本

    public string GetLua(string bundleName,string name)
    {
        if(!string.IsNullOrEmpty(name))name = (bundleName.ToString() +"/"+ name.ToLower()).ToLower();
        else name= bundleName;
        if (!luaTable.ContainsKey(packageName))
        {
            if(GameEnvMrg.Instance.isLocal)
            {
                Debug.LogError(packageName + " 还没有加载....");
                return null;
            }
            else
            {
                luaTable.Add(packageName, new Dictionary<string, string>());
            }
        }
        Dictionary<string, string> luaScripts = luaTable[packageName];
        if (!luaScripts.ContainsKey(name))
        {
            if (GameEnvMrg.Instance.isLocal)
            {
                Debug.LogError("Lua脚本" + name + "没有找到");
                return null;
            }
            else
            {
                AssetBundle assetBundle = LoadMrg.Instance.LoadAssetBundleScripts(bundleName);
                LoadAssetBundle(assetBundle);
            }
        }
        return luaScripts[name];
    }
    public bool HasLua(string bundleName, string name)
    {
        if (!luaTable.ContainsKey(packageName)) return false;
        name = (bundleName + "/" + name).ToLower();
        Dictionary<string, string> luaScripts = luaTable[packageName];
        if (!luaScripts.ContainsKey(name))return false;
        return true;
    }
    #endregion

    #region 移除Lua脚本

    public void RemoveLua(string name)
    {
        if (luaTable.ContainsKey(name))  luaTable.Remove(name);
    }

    #endregion

}